// 伴我们打包
import esbuild from "esbuild";
// 处理命令行参数
import minimist from "minimist";
import { join, resolve,dirname } from "path";
import { fileURLToPath } from "url";
// import { require } from "module";

// 解析命令行参数
const args = minimist(process.argv.slice(2));
fileURLToPath(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const target = args._[0] || "reactivity";
const format = args.f || "iife"; // esm, cjs, iife
// const require=createRequire(import.meta.url);
console.log(__filename, __dirname);

const entry=resolve(__dirname, `../packages/${target}/src/index.ts`);
// const pkg = require(join(__dirname, `../packages/${target}/package.json`));

// esbuild.build({
//   entryPoints: [entry],
//   bundle: true,
//   format,
//   outfile: resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`),
//   sourcemap: true,
// //   globalName:pkg.buildOptions?.name,
// }).catch(() => process.exit(1)).then((ctx) => {
//   console.log("done");
//   return ctx.watch();
// });
esbuild.context({
  // options
   entryPoints: [entry],
  bundle: true,
  format,
  outfile: resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`),
  sourcemap: true,
  //  globalName:pkg.buildOptions?.name,
}).then((ctx) => {
  console.log("done");
  return ctx.watch();
}).catch(() => process.exit(1));